package com.spring.hx.config;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@Data
@PropertySource("classpath:/application.yml")
@ConfigurationProperties(prefix = "globalconfig")
public class GlobalConfig {
    private static Logger log = LoggerFactory.getLogger(GlobalConfig.class);
    private String tokenExpires;
    private int tokenExistSecs;
    public void setTokenExpires(String tokenExpires) {
        this.tokenExpires = tokenExpires;
        if(tokenExpires.toLowerCase().endsWith("s")){
            this.tokenExistSecs = Integer.parseInt(tokenExpires.substring(0,tokenExpires.length()-1));
            log.info("获取token存在秒数：{}s",tokenExistSecs);
        }else if(tokenExpires.toLowerCase().endsWith("m")){
            this.tokenExistSecs = 60 * Integer.parseInt(tokenExpires.substring(0,tokenExpires.length()-1));
            log.info("获取token存在秒数：{}s",tokenExistSecs);
        }else if(tokenExpires.toLowerCase().endsWith("h")){
            this.tokenExistSecs = 3600 * Integer.parseInt(tokenExpires.substring(0,tokenExpires.length()-1));
            log.info("获取token存在秒数：{}s",tokenExistSecs);
        }else {
            try {
                tokenExistSecs = Integer.parseInt(tokenExpires);
                log.info("获取token存在秒数：{}s",tokenExistSecs);
            }catch (Exception e){
                throw new RuntimeException("请检查yml配置：config.tokenExpires");
            }
            log.info("获取token存在秒数：{}s",tokenExistSecs);
        }
    }

}
